layui.use(['table', 'admin', 'ax', 'func', 'upload', 'form', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;
    //文件上传
    var upload = layui.upload;

    //渲染时间选择框
    var laydate = layui.laydate;
    laydate.render({
        elem: '#inDate' //指定元素
    });

    laydate.render({
        elem: '#outDate' //指定元素
    });

    //执行文件上传实例
    $("#importData").click(function () {
        let relXqId = $('#relXqId').val()
        if (relXqId == "") {
            Feng.error("请先选择小区")
            return;
        }
    })


    function sqidIs() {
        upload.render({
            elem: '#importData' //绑定元素
            , url: Feng.ctxPath + '/ywhPersonInfo/importData_new' //上传接口
            , acceptMime: 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            , exts: 'xls|xlsx'
            , data: {
                relXqId: $("#relXqId").val()
            }
            , done: function (res) {
                // console.log(res)
                Feng.success("导入成功")
                table.reload(YwhPersonInfo.tableId);
            }
            , error: function () {
                //请求异常回调
            }
        });
    }

    form.on('select(relXqId)', function (data) {
        if (data.value != "") {
            sqidIs();
        } else {
            window.location.href = Feng.ctxPath + '/ywhPersonInfo'
        }
    });

    //下载导入模板
    $("#downloadModel").click(function () {
        window.location = Feng.ctxPath + '/data/downloadFile?id=' + 5
    })

    $("#btnDelete").click(function () {
        var checkRows = table.checkStatus(YwhPersonInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请至少选择一条数据");
            return;
        }
        var operation = function () {
            var ids = new Array();
            for (var i = 0; i < checkRows.data.length; i++) {
                ids.push(checkRows.data[i].id)
            }
            // console.log(ids)
            $.ajax(Feng.ctxPath + '/ywhPersonInfo/allDelete', {
                data: {
                    ids: ids,
                },
                traditional: true,
                success: function (data) {
                    // console.log(data)
                    if (data == ids.length) {
                        Feng.success('删除成功')
                        table.reload(YwhPersonInfo.tableId)
                    }
                }
            })
        }
        Feng.confirm("确定是否删除?", operation);
    })

    $.ajax(Feng.ctxPath + '/xqInfo/listAll', {
        success: function (res) {
            // console.log(res)
            $("#relXqId").empty();
            $("#relXqId").append("<option value=''>请选择小区</option>");
            for (let i = 0; i < res.length; i++) {
                $("#relXqId").append(new Option(res[i].xqName, res[i].id))
            }
            //重新渲染
            form.render("select");
        }
    })


    $.ajax(Feng.ctxPath + '/personInfo/listAll', {
        success: function (res) {
            // console.log(res)
            $("#relPersonId").empty();
            $("#relPersonId").append("<option value=''>请选择人员</option>");
            for (let i = 0; i < res.length; i++) {
                $("#relPersonId").append(new Option(res[i].personName + " (" + res[i].cardType + ':' + res[i].cardCode + ")", res[i].id))
            }
            //重新渲染
            form.render("select");
        }
    })

    /**
     * 业委会人员表管理
     */
    var YwhPersonInfo = {
        tableId: "ywhPersonInfoTable"
    };

    /**
     * 初始化表格的列
     */
    YwhPersonInfo.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'isDel', sort: false, hide: true, title: '0正常 1删除'},
            {field: 'relXqId', sort: false, hide: true, title: '小区id'},
            {field: 'relPersonId', sort: false, hide: true, title: '人员id'},
            {field: 'xqName', sort: false, title: '小区'},
            {
                field: 'personName', sort: false, title: '人员', templet: function (res) {
                    return res.personName + " (" + res.cardType + ':' + res.cardCode + ")";
                }
            },
            {field: 'jobDesc', sort: false, title: '职务'},
            {field: 'inDate', sort: false, title: '入职时间'},
            {field: 'outDate', sort: false, title: '解聘时间'},
            {
                field: 'state', sort: false, title: '人员在职情况', templet: function (res) {
                    if (res.state == 0) {
                        return "解聘"
                    }
                    if (res.state == 1) {
                        return "业委会成员"
                    }
                }
            },
            {field: 'createPerson', sort: false, title: '创建人'},
            {field: 'createDate', sort: false, title: '创建时间'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    YwhPersonInfo.search = function () {
        var queryData = {};

        queryData['id'] = $('#id').val();
        queryData['isDel'] = $('#isDel').val();
        queryData['relXqId'] = $('#relXqId').val();
        queryData['relPersonId'] = $('#relPersonId').val();
        queryData['jobDesc'] = $('#jobDesc').val();
        queryData['inDate'] = $('#inDate').val();
        queryData['outDate'] = $('#outDate').val();
        queryData['state'] = $('#state').val();
        queryData['createPerson'] = $('#createPerson').val();
        queryData['createDate'] = $('#createDate').val();

        table.reload(YwhPersonInfo.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 跳转到添加页面
     */
    YwhPersonInfo.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/ywhPersonInfo/add'
    };

    /**
     * 跳转到编辑页面
     *
     * @param data 点击按钮时候的行数据
     */
    YwhPersonInfo.jumpEditPage = function (data) {
        window.location.href = Feng.ctxPath + '/ywhPersonInfo/edit?id=' + data.id
    };

    /**
     * 导出excel按钮
     */
    YwhPersonInfo.exportExcel = function () {
        var checkRows = table.checkStatus(YwhPersonInfo.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    YwhPersonInfo.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/ywhPersonInfo/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(YwhPersonInfo.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + YwhPersonInfo.tableId,
        url: Feng.ctxPath + '/ywhPersonInfo/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: YwhPersonInfo.initColumn(),
        skin: 'line',
        done: function (res, curr, count) {
            $('tr').css('background-color', '#ffffff');
        }
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        YwhPersonInfo.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

        YwhPersonInfo.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        YwhPersonInfo.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + YwhPersonInfo.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            YwhPersonInfo.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            YwhPersonInfo.onDeleteItem(data);
        }
    });
});
